//js获取参数
var getQueryString = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
};
var mid = getQueryString("m");
$.ajax({
	url : "currentuser",
	dataType : "json",
	success : function(data) {
		var updateUserInfo = function(user) {
			$(".username").text("");
			$(".username").text(user);
		};
		var createMenuli = function(menu,data) {
			if(data.length == 0){
				alert("您没有访问权限！");
				window.opener = null;
				window.open('','_self','');
				window.close();
			}else{
				for (var i = 0; i < data.length; i++) {
					var nav = data[i];
					var li = $("<li id='menu" + nav.moduleId + "'></li>");
					if (nav.children.length > 0) {
						li.append("<a href='#' class='dropdown-toggle'>\
									<i class="+nav.moduleClass+"></i><span class='menu-text'>"
									+ nav.moduleName + "</span><b class='arrow icon-angle-down'></b></a>");
						var ul = $("<ul class='submenu'></ul>");
						createMenuli(ul, nav.children);
						li.append(ul);
					} else {
						li.append("<a href=" + nav.modulePath + "?m=" + nav.moduleId + ">\
									<i class="+nav.moduleClass+"></i><span class='menu-text'>"
									+ nav.moduleName + "</span></a>");
					}
					menu.append(li);
				}
			}
		};
		var selectMenuli = function(uid){
			if(uid.parent().attr("id")=="menu"){
				uid.addClass("active");
			}else{
				uid.addClass("active");
				uid.parent().parent().addClass("active open");
				selectMenuli(uid.parent().parent());
			}
			var mname = uid.find("span").eq(0).text();
			$(".breadcrumb").append("<li>"+mname+"</li>");
		};
		if (data.user != undefined) {
			updateUserInfo(data.user);
			createMenuli($("#menu"), data.menu);
			if(mid!=null){
				showOperationBtn(mid);
				selectMenuli($("#menu"+mid));
			}
		}
	},
	error : function(ex){
		console.log(ex);
	}
});
//getCookie
var getCookie = function(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1)
				c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
};
//setCookie
var setCookie = function(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name
			+ "="
			+ escape(value)
			+ ((expiredays == null) ? "" : ";expires="
					+ exdate.toGMTString());
};
var tableload = function(tid, url, dataMap, columns, length){
	if(url==undefined)return;
	if(dataMap==undefined)dataMap=null;
	if(columns==undefined)columns=null;
	if(length==undefined)length=15;
	$("#" + tid).dataTable({
		"dom" : "<'toolbar'>frtip",
		"autoWidth" : false,
		"searching" : false,
		"ordering" : false,
		"stateSave" : true,
		"processing" : true,
		"serverSide" : true,
		"ajax": {
            "url": url,
            "type": "POST",
            "data": dataMap,
            "error":function(ex){
            	console.log(ex);
            	if(ex.status==200){
            		window.location.href=window.location.href;
            	}
            }
        },
		"columns" : columns,
		"lengthMenu" : [[length],[length]],
		"language" : {
			"lengthMenu": "每页 _MENU_ 条记录",
			"info" : "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
			"infoEmpty" : "",
			"infoFiltered": "",
			"zeroRecords" : "没有检索到数据",
			"processing" : "<i class='icon-spinner icon-spin icon-2x pull-left'></i>"
		}
	});
	$("div.toolbar").html("").append("<p>\
			<button class='btn btn-sm btn-primary add' style='display:none'><i class='icon-plus'></i>添加</button>\
			<button class='btn btn-sm btn-success edit' style='display:none'><i class='icon-pencil'></i>编辑</button>\
			<button class='btn btn-sm btn-warning del' style='display:none'><i class='icon-trash'></i>删除</button>\
		</p>");
	
	$(".add").click(_addFun);
	$(".edit").click(_editFun);
	$(".del").click(_delFun);
};
var showOperationBtn = function(moduleid){
	$.ajax({
		type : "post",
		url : "modulebtn",
		data : {mid:moduleid},
		success : function(list) {
			for(var i = 0; i < list.length; i++){
				$("."+list[i].operationCode).show();
			}
		},
		error: function(ex){
        	console.log(ex);
        }
	});	
};
var setselectval = function(sid, url, key, name, dataMap) {
	if(sid==undefined)return;
	if(url==undefined)return;
	if(key==undefined)return;
	if(name==undefined)return;
	$.ajax({
		type : "post",
		url : url,
		data : dataMap,
		success : function(list) {
			$("#"+sid).html("");
			for (var i = 0; i < list.length; i++) {
				$("#"+sid).append("<option value ='"+list[i][key]+"'>"+list[i][name]+"</option>");
			}
		},
		error: function(ex){
        	console.log(ex);
        }
	});	
};
var showDelMessage = function(data){
	if(data.valid){
		alert("删除成功！");
	}else{
		alert("该数据已使用，不能删除！");
	}
};
//按指定格式格式化日期
Date.prototype.format = function(format) {
    var o =
    {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};
//返回json数据
$.ajaxjson = function (url, dataMap, fnSuccess) {
    $.ajax({
        type: "POST",
        url: url,
        data: dataMap,
        dataType: "json",
        success: fnSuccess,
        error: function(ex){
        	console.log(ex);
        }
    });
};